const topic = {
  namespaced: true,
  state() {
    return {
      // 题页状态
      status: {
        // 上一题
        prev: false,
        // 下一题
        next: false,
        // 题次
        topicIndex: 1,
      },
      //当前题
      current: 0,
      //答题卡--选项集
      selectGather: [],
    };
  },
  mutations: {
    // 当前题
    handleCurrent(state, response) {
      state.current = response;
    },
    // 修改题页状态
    handleTopicStatus(state, response) {
      if (response.type === "next") {
        //下一题
        state.status.next = !state.status.next;
      } else if (response.type === "prev") {
        //上一题
        state.status.prev = !state.status.prev;
      } else {
        //手动翻页
        state.status.topicIndex = response.value;
      }
    },
    // 作答及写入选项集
    handelRespond(state, response) {
      // 题号，答案选项
      const { id, select, type } = response;
      console.log(id, select, type);
      // 判断选项集是否已存在此题的选项记录
      const isId = state.selectGather.findIndex((item) => item.id === id);
      if (type === "one" || type === "judge") {
        // 单选 或 判断
        console.log(isId);
        if (isId != -1) {
          // 有选
          state.selectGather[isId].select = select;
        } else {
          //初选
          state.selectGather.push({ id, select });
        }
      } else if (type === "more") {
        //多选
        if (isId != -1) {
          // 有选
          // 已选数量
          let selcetLength = state.selectGather[isId].select.length;
          // 已有集合
          let tempSelcetArr = new Set(state.selectGather[isId].select);
          // 判断集合里是否已有此值&&至少有一个值
          if (tempSelcetArr.has(select) && selcetLength > 1) {
            // 删除已选值
            tempSelcetArr.delete(select);
          } else {
            // 添加已选值
            tempSelcetArr.add(select);
          }
          // 修改选项集
          state.selectGather[isId].select = Array.from(tempSelcetArr);
        } else {
          //初选
          // 直接写入选项集
          state.selectGather.push({ id, select: [select] });
        }
      }
    },
    // 重置
    handleReset(state) {
      (state.status = {
        prev: false,
        next: false,
        topicIndex: 1,
      }),
        (state.selectGather = []),
        (state.current = 0);
    },
  },
};
export default topic;
